import { defineStore } from 'pinia'
import axios from '@/api'
import { ref } from 'vue'


export default defineStore('creditLoans', () => {
    // 获取产品列表
    let productList = ref<Array<any>>([]);
    let getProduct = async () => {
        const data = await axios.get<any>('/loan/products');
        productList.value = data.list.map((item: any) => {
            const link = {
                1: '/pages/first-room/first-room',
                2: '/pages/second-room/second-room',
                3: '/pages/credit-loans/credit-loans',
                4: '/pages/credit-loans/credit-loans',
                5: '/pages/second-room/second-room',
                6: '/pages/second-room/second-room'
            }
            return {
                ...item,
                link: link[item.id as keyof typeof link] + `?id=${item.id}`
            }
        })
    }
    getProduct()
    // 获取产品详情
    let getProductdetail = (id: string | number) => {
        return axios.get(`/loan/product/${id}`)
    }
    // 申请
    let apply = (data: any) => {
        return axios.post('/loan/apply', data, true)
    }
    // 选择业务员
    let chose_sm = (data: any) => {
        return axios.post('/loan/chose_sm', data)
    }

    const objToList = (obj: Object) => {
        return Object.entries(obj).map(item => ({ label: item[1], value: item[0] }))
    }
    const 进阶条件 = { house: '有房族', work: '工薪族' };
    const 征信状况 = { Y: '优', L: '良', C: '差' };
    const 贷款年限 = [5, 10, 15, 20, 25, 30];
    const 房屋用途 = { jz: "居住", fjz: "非居住" };
    const 房屋属性 = { spf: "商品房", fgf: "房改房", flf: "福利房" };
    const 婚姻状况 = { 'single': '未婚', 'married': '已婚', 'divorced': '离异', 'widowed': '丧偶' }
    const 订单状态 = { wait: '待处理', wait_pay: '待付款', payed: '已付款', complete: '已完成', reject: '已拒绝' }
    const 业务员订单状态 = { '': '全部', wait_sub: '待提交', wait_td: '待勘验', wait_sign: '待签约', wait_pay: '待付款', complete: '已完成' }
    // 一手房申请
    let ysfApply = (data: any) => {
        return axios.file('/loan/ysfApply', data, { fileNames: ['id_photo_front', 'id_photo_back'], toast: true })
    }

    // 税费计算器
    let taxes_and_fees = (data: any) => {
        return axios.get('/cal/taxes_and_fees', data, true)
    }
    // 业务圆验证
    let queryinfo = (num: number | string) => {
        return axios.get('/salesman/queryinfo', { mobile: num }, true)
    }

    function toNextPage(loan: any, is_ysf: number = 0, salesman_id: string,) {
        if (loan.salesman_id) {
            // 去订单详情
            uni.navigateTo({
                url: `/pages/application-record/detail?loan_id=${loan.id}&is_ysf=${is_ysf}`
            })
        } else if (salesman_id) {
            chose_sm({
                loan_id: loan.id,
                is_ysf,
                salesman_id
            }).then(() => {
                uni.navigateTo({
                    url: `/pages/application-record/${is_ysf == 1 ? 'ysf-detail' : 'detail'}?loan_id=${loan.id}&is_ysf=${is_ysf}`
                })
            })
        } else {
            // 选择业务员
            uni.navigateTo({
                url: `/pages/credit-loans/salesman?loan_id=${loan.id}&is_ysf=${is_ysf}`
            })
        }
    }
    return {
        productList, getProductdetail, apply, chose_sm, objToList,
        进阶条件,
        征信状况,
        贷款年限,
        房屋用途,
        房屋属性,
        婚姻状况,
        订单状态,
        业务员订单状态,
        ysfApply,
        getProduct,
        taxes_and_fees,
        queryinfo,
        toNextPage
    }
})

